package com.toyi.util;

/**
 * This class does string tokenizing.
 * The difference between this class and java.util.StringTokenizer is that
 * the delimiter is only one char instead of few.
 */
public class SimpleStringTokenizer
{
	private boolean _tranverse;
	private int _currentIndex;
	private int _nextIndex;
	private int _stringLen;
	private char _delimiter;
	private String _s;

	//---------------------------------------------------------------------------------------------
	// Constructors
	//---------------------------------------------------------------------------------------------
	/**
	 * Constructs a SimpleStringTokenizer.
	 * @param s the string to tokenize.
	 * @param delimiter delimiter to tokenize the string.
	 */
	public SimpleStringTokenizer(String s, char delimiter)
	{
		_currentIndex = 0;
		_delimiter = delimiter;
		_nextIndex = 1;
		_s = s;
		_tranverse = false;
		try
		{
			_stringLen = _s.length();
		}
		catch (Exception exc)
		{
			_stringLen = 0;
		}
	}

	//---------------------------------------------------------------------------------------------
	// Class Methods
	//---------------------------------------------------------------------------------------------
	/**
	 * Returns the number of tokens.
	 * @return The number of tokens.
	 */
	public int countTokens()
	{
		int tokens = 0;
		int storedCurrentIndex = _currentIndex;
		int storedNextIndex = _nextIndex;
		try
		{
			while (hasMoreTokens())
			{
				nextToken();
				tokens++;
			}
			_currentIndex = storedCurrentIndex;
			_nextIndex = storedNextIndex;
			_tranverse = false;
			return tokens;
		}
		catch (Exception exc)
		{
			return 0;
		}
	}

	/**
	 * Returns whether there is more tokens to be retrieved.
	 * @return True if there is more tokens, false otherwise.
	 */
	public boolean hasMoreTokens()
	{
		try
		{
			if (!_tranverse)
			{
				tranverse();
				_tranverse = true;
			}
			return (_currentIndex < _stringLen);
		}
		catch (Exception exc)
		{
			return false;
		}
	}

	/**
	 * Returns the next token.
	 * @return The next token.
	 */
	public String nextToken()
	{
		if (!_tranverse) tranverse();
		if (_nextIndex < 0) _nextIndex = _stringLen;
		String substr = _s.substring(_currentIndex, _nextIndex);
		_currentIndex = _nextIndex + 1;
		_tranverse = false;
		return substr;
	}

	private void tranverse()
	{
		_nextIndex = _s.indexOf(_delimiter, _currentIndex);
		while (_nextIndex == _currentIndex && _currentIndex < _stringLen)
		{
			_currentIndex = _nextIndex + 1;
			_nextIndex = _s.indexOf(_delimiter, _currentIndex);
		}
	}

/*
	public static void main(String[] args)
	{
		// Running test cases for this class
		System.out.println("Running tests for SimpleStringTokenizer");
		System.out.println("Delimiter = [.]");

		String s = "apple.orange.banana.grapes";
		char delimiter = '.';


		System.out.println("Test 1");
		System.out.println("String = ["+s+"]");
		SimpleStringTokenizer st = new SimpleStringTokenizer(s, delimiter);
		System.out.println("No of tokens = "+st.countTokens());
		while (st.hasMoreTokens())
		{
			System.out.println("Token = ["+st.nextToken()+"]");
		}
		System.out.println();

		s = ".zoo..garden...park...";
		System.out.println("Test 2");
		System.out.println("String = ["+s+"]");
		st = new SimpleStringTokenizer(s, delimiter);
		System.out.println("No of tokens = "+st.countTokens());
		while (st.hasMoreTokens())
		{
			System.out.println("Token = ["+st.nextToken()+"]");
		}
		System.out.println();

		s = "hello";
		System.out.println("Test 3");
		System.out.println("String = ["+s+"]");
		st = new SimpleStringTokenizer(s, delimiter);
		System.out.println("No of tokens = "+st.countTokens());
		while (st.hasMoreTokens())
		{
			System.out.println("Token = ["+st.nextToken()+"]");
		}
		System.out.println();

		s = "...";
		System.out.println("Test 4");
		System.out.println("String = ["+s+"]");
		st = new SimpleStringTokenizer(s, delimiter);
		System.out.println("No of tokens = "+st.countTokens());
		while (st.hasMoreTokens())
		{
			System.out.println("Token = ["+st.nextToken()+"]");
		}
		System.out.println();

		s = ".  . .  ";
		System.out.println("Test 5");
		System.out.println("String = ["+s+"]");
		st = new SimpleStringTokenizer(s, delimiter);
		System.out.println("No of tokens = "+st.countTokens());
		while (st.hasMoreTokens())
		{
			System.out.println("Token = ["+st.nextToken()+"]");
		}
		System.out.println();

		s = ".  . .";
		System.out.println("Test 6");
		System.out.println("String = ["+s+"]");
		st = new SimpleStringTokenizer(s, delimiter);
		System.out.println("No of tokens = "+st.countTokens());
		while (st.hasMoreTokens())
		{
			System.out.println("Token = ["+st.nextToken()+"]");
		}
		System.out.println();

		s = " .  . .  ";
		System.out.println("Test 7");
		System.out.println("String = ["+s+"]");
		st = new SimpleStringTokenizer(s, delimiter);
		System.out.println("No of tokens = "+st.countTokens());
		while (st.hasMoreTokens())
		{
			System.out.println("Token = ["+st.nextToken()+"]");
		}
		System.out.println();

		s = "";
		System.out.println("Test 8");
		System.out.println("String = ["+s+"]");
		st = new SimpleStringTokenizer(s, delimiter);
		System.out.println("No of tokens = "+st.countTokens());
		while (st.hasMoreTokens())
		{
			System.out.println("Token = ["+st.nextToken()+"]");
		}
		System.out.println();

		s = null;
		System.out.println("Test 9");
		System.out.println("String = ["+s+"]");
		st = new SimpleStringTokenizer(s, delimiter);
		System.out.println("No of tokens = "+st.countTokens());
		while (st.hasMoreTokens())
		{
			System.out.println("Token = ["+st.nextToken()+"]");
		}
		System.out.println();

		// Profiling against java.util.StringTokenizer
		System.out.println("Profiling against java.util.StringTokenizer");
		s = "ab.bc.cd.de";
		String delimiterStr = ".";
		long start = System.currentTimeMillis();
		for (int i=0; i < 10000000; i++)
		{
			java.util.StringTokenizer javast = new java.util.StringTokenizer(s, delimiterStr);
//			javast.countTokens();
			while (javast.hasMoreTokens()) javast.nextToken();
//			for (int j=0; j < javast.countTokens(); j++) javast.nextToken();
		}
		long stop = System.currentTimeMillis();
		System.out.println("java StringTokenizer takes "+(stop-start)+" ms");

		start = System.currentTimeMillis();
		for (int i=0; i < 10000000; i++)
		{
			st = new SimpleStringTokenizer(s, delimiter);
//			st.countTokens();
			while (st.hasMoreTokens()) st.nextToken();
//			for (int j=0; j < st.countTokens(); j++) st.nextToken();
		}
		stop = System.currentTimeMillis();
		System.out.println("SimpleStringTokenizer takes "+(stop-start)+" ms");
	}
*/
}
